package com.example.service;

import com.example.dao.UserRepository;
import com.example.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserRepository userRepository;

    //查询余额
    @Override
    public int balance(User user) {
        User u = userRepository.selectmoney(user.getId());
        return u.getMoney();
    }

    //消费
    @Override
    public void consume(User user, int money) {
        int money_before = balance(user);
        if (money_before >= money) {
            userRepository.addconsumeordrawback(user.getId(), "-" + money, new Date());
            int money_after = money_before - money;
            userRepository.updatemoney(user.getId(), money_after);
        } else {
            System.out.println("余额不足");
        }
    }

    //退款
    @Override
    public void drawback(User user, int money) {
        int money_before = balance(user);
        userRepository.addconsumeordrawback(user.getId(), "+" + money, new Date());
        int money_after = money_before + money;
        userRepository.updatemoney(user.getId(), money_after);
    }

    //查询用户钱包金额变动明细
    @Override
    public List<User> selectconsumeordrawback(User user) {
        List<User> users = userRepository.selectconsumeordrawback(user.getId());
        for (User u : users) {
            System.out.println("用户id："+u.getId()+"   交易金额:"+u.getConsumeordrawback()+"    交易时间："+u.getAddtime());
        }
        return users;
    }


}
